﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Transactions;
using DataAccess;
using Common;

#region VƯƠNG TRÍ LỢI
namespace BusinessLogic
{
    public class ChiTietChinhSachGiaHangHoa : MarshalByRefObject
    {
        private static SqlCommand cmd;
        private static ArrayList arr;
        private static SqlDataReader dr;
        private static SqlConnection cn;

        /// <summary>
        /// select
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static Entities.ChiTietChinhSachGiaHangHoa[] Select()
        {
            try
            {
                string sql = Common.Constants.ChiTietChinhSachGiaHangHoa.Select;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietChinhSachGiaHangHoa row = new Entities.ChiTietChinhSachGiaHangHoa();
                    row.MaChinhSachGiaHangHoa = (dr[Common.Constants.ChiTietChinhSachGiaHangHoa.MaChinhSachGiaHangHoa].ToString());
                    row.MaHangHoa = dr[Common.Constants.ChiTietChinhSachGiaHangHoa.MaHangHoa].ToString();
                    row.TenHangHoa = (dr[Common.Constants.ChiTietChinhSachGiaHangHoa.TenHangHoa].ToString());
                    row.SoLuong = int.Parse(dr[Common.Constants.ChiTietChinhSachGiaHangHoa.Soluong].ToString());
                    row.PhanTramGiaBanBuon = dr[Common.Constants.ChiTietChinhSachGiaHangHoa.PhanTramGiaBanBuon].ToString();
                    row.PhanTramGiaBanLe = dr[Common.Constants.ChiTietChinhSachGiaHangHoa.PhanTramGiaBanLe].ToString();
                    row.GhiChu = dr[Common.Constants.ChiTietChinhSachGiaHangHoa.GhiChu].ToString();
                    row.Deleted = bool.Parse(dr[Common.Constants.ChiTietChinhSachGiaHangHoa.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return null;

                Entities.ChiTietChinhSachGiaHangHoa[] arrC = new Entities.ChiTietChinhSachGiaHangHoa[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.ChiTietChinhSachGiaHangHoa)arr[i];
                }

                //Giai phong bo nho
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return arrC;
            }
            catch { return new Entities.ChiTietChinhSachGiaHangHoa[0]; }
        }

        /// <summary>
        /// insert
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static bool InsertUpdate(Entities.ChiTietChinhSachGiaHangHoa chitietchinhsachgiahanghoa)
        {
            TransactionScope ts = null;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.ChiTietChinhSachGiaHangHoa.InsertUpdate;
                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(sql, cn);

                    cmd.Parameters.Add(Common.Constants.ChiTietChinhSachGiaHangHoa.HanhDong, SqlDbType.NVarChar, 50).Value = chitietchinhsachgiahanghoa.HanhDong;
                    cmd.Parameters.Add(Common.Constants.ChiTietChinhSachGiaHangHoa.MaChinhSachGiaHangHoa, SqlDbType.VarChar, 50).Value = chitietchinhsachgiahanghoa.MaChinhSachGiaHangHoa;
                    cmd.Parameters.Add(Common.Constants.ChiTietChinhSachGiaHangHoa.MaHangHoa, SqlDbType.VarChar, 50).Value = chitietchinhsachgiahanghoa.MaHangHoa;
                    cmd.Parameters.Add(Common.Constants.ChiTietChinhSachGiaHangHoa.TenHangHoa, SqlDbType.NVarChar, 200).Value = chitietchinhsachgiahanghoa.TenHangHoa;
                    cmd.Parameters.Add(Common.Constants.ChiTietChinhSachGiaHangHoa.Soluong, SqlDbType.Int).Value = chitietchinhsachgiahanghoa.SoLuong;
                    cmd.Parameters.Add(Common.Constants.ChiTietChinhSachGiaHangHoa.PhanTramGiaBanBuon, SqlDbType.Float).Value = chitietchinhsachgiahanghoa.PhanTramGiaBanBuon;
                    cmd.Parameters.Add(Common.Constants.ChiTietChinhSachGiaHangHoa.PhanTramGiaBanLe, SqlDbType.Float).Value = chitietchinhsachgiahanghoa.PhanTramGiaBanLe;
                    cmd.Parameters.Add(Common.Constants.ChiTietChinhSachGiaHangHoa.GhiChu, SqlDbType.NVarChar, 200).Value = chitietchinhsachgiahanghoa.GhiChu;
                    cmd.Parameters.Add(Common.Constants.ChiTietChinhSachGiaHangHoa.Deleted, SqlDbType.Bit).Value = chitietchinhsachgiahanghoa.Deleted;

                    int i = cmd.ExecuteNonQuery();
                    bool kt = false;
                    if (i == 1)
                        kt = true;
                    else
                        kt = false;
                    cmd.Connection.Dispose();
                    cn.Close();
                    cn = null;

                    ts.Complete();
                    return kt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message;
                return false;
            }
        }

        public static bool InsertUpdate(Entities.ChiTietChinhSachGiaHangHoa[] chitietchinhsachgiahanghoa)
        {
            try
            {
                for (int i = 0; i < chitietchinhsachgiahanghoa.Length; i++)
                {
                    InsertUpdate(chitietchinhsachgiahanghoa[i]);
                }
                return true;
            }
            catch
            { return false; }
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static bool Delete(Entities.ChiTietChinhSachGiaHangHoa chitietchinhsachgiahanghoadel)
        {
            try
            {
                bool kt = false;
                string sql = Common.Constants.ChiTietChinhSachGiaHangHoa.Delete;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);

                cmd.Parameters.Add(Common.Constants.ChiTietChinhSachGiaHangHoa.MaChinhSachGiaHangHoa, SqlDbType.VarChar).Value = chitietchinhsachgiahanghoadel.MaChinhSachGiaHangHoa;
                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    kt = true;
                else
                    kt = false;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return kt;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return false;
            }
        }
    }
}
#endregion